#!/usr/bin/env bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2018-2019, Intel Corporation
#
#
# pmempool_sync_remote/TEST35 -- pmempool sync with remote replication
#                                and bad blocks in the remote replica:
#                                - the pool consists of 1 part
#                                - offset of bad block: 1000
#

. ../unittest/unittest.sh

require_test_type medium
require_build_type debug nondebug
require_linked_with_ndctl $PMEMPOOL$EXESUFFIX

require_nodes 2
require_badblock_tests_enabled_node 1

setup

require_node_libfabric 0 $RPMEM_PROVIDER
require_node_libfabric 1 $RPMEM_PROVIDER

init_rpmem_on_node 0 1

require_node_log_files 0 pmemobj$UNITTEST_NUM.log
require_node_log_files 0 pmempool$UNITTEST_NUM.log
require_node_log_files 1 pmemobj$UNITTEST_NUM.log
require_node_log_files 1 pmempool$UNITTEST_NUM.log

. ../common_badblock.sh

MOUNT_DIR="${NODE_DIR[1]}/mnt-pmem"
badblock_test_init_node 1 block_device $MOUNT_DIR

LAYOUT="layout"

LOG=out${UNITTEST_NUM}.log
rm -f $LOG && touch $LOG

POOLSET_LOCAL="local_pool.set"
POOLSET_REMOTE="remote_pool.set"

create_poolset $DIR/$POOLSET_LOCAL \
	8M:${NODE_DIR[0]}/pool.local:x \
	M \
	${NODE_ADDR[1]}:$POOLSET_REMOTE

create_poolset $DIR/$POOLSET_REMOTE \
	8M:$MOUNT_DIR/pool.remote:x

copy_files_to_node 0 ${NODE_DIR[0]} $DIR/$POOLSET_LOCAL
copy_files_to_node 1 ${NODE_DIR[1]} $DIR/$POOLSET_REMOTE

expect_normal_exit run_on_node 0 ../pmempool rm -sf ${NODE_DIR[0]}$POOLSET_LOCAL
expect_normal_exit run_on_node 0 "../obj_verify ${NODE_DIR[0]}$POOLSET_LOCAL $LAYOUT c v &>> $LOG"

###############################################################################
# Enabling features in poolsets with remote replicas is not supported now,
# so we have to use the following workaround to enable
# the POOL_FEAT_CHECK_BAD_BLOCKS compat feature in such poolset.
# We create the auxiliary poolset without the remote replica
# and enable this compat feature on all nodes separately.

POOLSET_LOCAL_AUX=${POOLSET_LOCAL}_aux

create_poolset $DIR/${POOLSET_LOCAL_AUX} \
	8M:${NODE_DIR[0]}/pool.local:x

copy_files_to_node 0 ${NODE_DIR[0]} $DIR/${POOLSET_LOCAL_AUX}

turn_on_checking_bad_blocks_node 0 ${NODE_DIR[0]}/${POOLSET_LOCAL_AUX}
turn_on_checking_bad_blocks_node 1 ${NODE_DIR[1]}/${POOLSET_REMOTE}

###############################################################################

# inject bad block:
FILE=$MOUNT_DIR/pool.remote
FIRST_SECTOR=$(expect_normal_exit run_on_node 1 ../extents $FILE -l 1000)

ndctl_inject_error_node 1 $NAMESPACE $FIRST_SECTOR 1

expect_bad_blocks_node 1 $NAMESPACE

# verification should fail
expect_abnormal_exit run_on_node 0 "../obj_verify ${NODE_DIR[0]}/$POOLSET_LOCAL $LAYOUT v &>> $LOG"

expect_bad_blocks_node 1 $NAMESPACE

# the remote replica contains bad blocks, so it cannot be opened and will be recreated
expect_normal_exit run_on_node 0 "../pmempool sync -b -v ${NODE_DIR[0]}/$POOLSET_LOCAL &>> $LOG"

# verification should succeed
expect_normal_exit run_on_node 0 "../obj_verify ${NODE_DIR[0]}/$POOLSET_LOCAL $LAYOUT v &>> $LOG"

ndctl_uninject_error_node 1 $FULLDEV $NAMESPACE $FIRST_SECTOR 1
badblock_test_fini_node 1 $MOUNT_DIR

pass
